/**
 * 
 */
package com.example.rabbitmqTest;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import com.example.rebbitmqdelayTest.OrderTradeRecord;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;  
/**mq监听器:消息确认消费模式 
 * @author yanglei
 * 2018年1月15日
 */
@Component
public class SecondMqListener implements ChannelAwareMessageListener{  
  
    private static final Logger log= LoggerFactory.getLogger(SecondMqListener.class);  
  
    @Autowired  
    private ObjectMapper objectMapper;  
    
    @RabbitListener(queues = "${mq.second.queue.name}",containerFactory = "singleListenerContainer",bindings={}) 
    public void consumeMessage(Message message, Channel channel){  
    	long deliveryTag=message.getMessageProperties().getDeliveryTag();
    	Map<String, Object> map=message.getMessageProperties().getHeaders();
    	String str=message.getMessageProperties().getReceivedRoutingKey();
    	String str1=message.getMessageProperties().getReceivedExchange();
        try {  
            log.debug("消费者监听交易记录信息： {} ",message);  
  
           
        }catch (Exception e){  
            log.error("消息体解析 发生异常； ",e.fillInStackTrace());  
        }  
    }  
    
    @Override  
    public void onMessage(Message message, Channel channel) throws Exception {  
        long deliveryTag=message.getMessageProperties().getDeliveryTag();  
        try {  
            /*byte[] body=message.getBody(); 
            log.debug("第二消息确认监听器-消费者 接收到消费发送的消息： {} ",new String(body,"UTF-8"));*/  
  
            byte[] body=message.getBody();  
            String entity=objectMapper.readValue(body, String.class);  
            log.debug("消息监听-消费者 接收到收款单信息：{} ",entity);  
  
            channel.basicAck(deliveryTag,false);  
        }catch (Exception e){  
            log.error("消息体解析异常：",e.fillInStackTrace());  
            channel.basicReject(deliveryTag,false);  
        }  
    }  
}  